package com.risen.test;

import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * @auther lmj
 * @date 2019/8/15
 */
// 读取以properties结尾的文件,获取key对应的value值
public class ReadProperties {
    private static final String CONFIG_PROPERTIES = "com/test/properties/test.properties";

    public static String getConfigResource(String key) throws IOException {
        ClassLoader loader = Thread.currentThread().getContextClassLoader();
        Properties properties = new Properties();
        InputStream in = loader.getResourceAsStream(CONFIG_PROPERTIES);
        properties.load(in);
        String value = properties.getProperty(key);
        value = new String(value.getBytes("ISO-8859-1"), "UTF-8");
        value = value.trim();
        in.close();
        return value;
    }

    @Test
    public void getProperties() {
        try {
            String name = ReadProperties.getConfigResource("name");
            System.out.println("获取到的name属性值为：" + name);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
